package UserService;

import JDBC.DBHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImp implements UserService {
    @Override
    public  User  finduser(String uname) {
            Connection conn= DBHelper.getConn();
            String sql="select * from user WHERE uname=?;";
            PreparedStatement stat=null;
            ResultSet rs=null;
            User u=new User();
         //   u=null;
            try {
                stat=conn.prepareStatement(sql);
                stat.setString(1,uname);
                rs=stat.executeQuery();//executeQuery该方法用来执行查询语句
                while(rs.next()){
                    u.setUname(rs.getString("uname"));
                    u.setPassword(rs.getString("password"));
                    u.setPhone(rs.getString("phone"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                DBHelper.closeAll(conn,stat,rs);
            }
        return u;
        }


    @Override
    public void createNewUser(String uname, String password,String phone) {
        Connection conn=DBHelper.getConn();
        String sql="INSERT  user(uname,password,phone) VALUE (?,?,?);";
        PreparedStatement stat=null;
        ResultSet rs=null;
        try{
            stat=conn.prepareStatement(sql);
            stat.setString(1, uname);
            stat.setString(2, password);
            stat.setString(3, phone);
            stat.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBHelper.closeAll(conn,stat,rs);
        }
    }
//
//    @Override
//    public void updateUser(User user) {
//        Connection conn=DBHelper.getConn();
//        //  String sql = "UPDATE User SET name='" + user.getName()+"',password='"+user.getPassword()+"', balance = "+user.getBalance()+" WHERE id='"+user.getID()+"';";
//        String sql ="UPDATE user SET uname=?,password=? WHERE sid=?";
//        PreparedStatement stat = null;
//        ResultSet rs=null;
//        try {
//            stat=conn.prepareStatement(sql);
//            stat.setString(1,user.getUname());
//            stat.setString(2,user.getPassword());
//            stat.setString(3,user.getSid());
//
//            stat.executeUpdate();
//
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }finally {
//            DBHelper.closeAll(conn,stat,rs);
//        }
//
//    }
//
//    @Override
//    public int getSerial() {
//        int serialNum = 0;
//        Connection conn=DBHelper.getConn();
//        String sql="select * from user Where  sid is null ;";
//        PreparedStatement stat=null;
//        ResultSet rs=null;
//        try {
//            stat=conn.prepareStatement(sql);
//            rs=stat.executeQuery();
//            while (rs.next()){
//                serialNum=rs.getInt("serialnum");
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }finally {
//            DBHelper.closeAll(conn,stat,rs);
//        }
//        return serialNum;
//    }
@Override
public void updateUser(User user,String username,String newpassword,String phone) {
    Connection conn=DBHelper.getConn();
// String sql = "UPDATE User SET name='" + user.getName()+"',password='"+user.getPassword()+"', balance = "+user.getBalance()+" WHERE id='"+user.getID()+"';";
    String sql ="UPDATE user SET uname=?,password=?,phone=? WHERE uname=?";
    PreparedStatement stat = null;
    ResultSet rs=null;
    try {
        stat=conn.prepareStatement(sql);
        String prename=user.getUname();
        System.out.println("111");
        stat.setString(1,username);
        stat.setString(2,newpassword);
        stat.setString(3,phone);
        stat.setString(4,prename);
        System.out.println("222");
        stat.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }finally {
        DBHelper.closeAll(conn,stat,rs);
    }
}
}
